package top.infopub.mgr.ic.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.ic.dao.MonthCtrlAttDao;
import top.infopub.mgr.ic.dao.MonthCtrlDao;
import top.infopub.mgr.ic.dao.MonthCtrlMileDao;
import top.infopub.mgr.ic.dao.MonthCtrlSubDao;
import top.infopub.mgr.ic.dao.MonthCtrlSubpackDao;
import top.infopub.mgr.ic.domain.MonthCtrlDomain;
import top.infopub.mgr.ic.service.MonthCtrlService;
import top.infopub.mgr.ic.vo.MonthCtrlVo;
import top.infopub.model.ic.MonthCtrl;
import top.infopub.model.ic.MonthCtrlAtt;
import top.infopub.model.ic.MonthCtrlMile;
import top.infopub.model.ic.MonthCtrlSub;
import top.infopub.model.ic.MonthCtrlSubpack;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class MonthCtrlServiceImpl extends GenericServiceImpl<MonthCtrl, String> implements
		MonthCtrlService {

	@Autowired
	private MonthCtrlDao monthCtrlDao;
	
	@Autowired
    private MonthCtrlSubpackDao monthCtrlSubpackDao;
    
    @Autowired
    private MonthCtrlSubDao monthCtrlSubDao;
    
    @Autowired
    private MonthCtrlMileDao monthCtrlMileDao;
    
    @Autowired
    private MonthCtrlAttDao monthCtrlAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<MonthCtrl, String> getDao() {
		return monthCtrlDao;
	}

    @Override
    public Page<MonthCtrlDomain> selectByDomainWhere(Page<MonthCtrlDomain> page,
                                                     MonthCtrlVo monthCtrlVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<MonthCtrlDomain>) monthCtrlDao.selectByDomainWhere(monthCtrlVo);
    }

    @Override
    public Page<MonthCtrlDomain> selectMyCheckByDomainWhere(Page<MonthCtrlDomain> page,
                                                            MonthCtrlVo monthCtrlVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<MonthCtrlDomain>) monthCtrlDao.selectMyCheckByDomainWhere(monthCtrlVo);
    }

    @Override
    public void saveMonthCtrl(MonthCtrl monthCtrl, List<MonthCtrlSubpack> monthCtrlSubpacks,
                              List<MonthCtrlSub> monthCtrlSubs,
                              List<MonthCtrlMile> monthCtrlMiles, List<MonthCtrlAtt> monthCtrlAtts) {
        //插入预立项记录
        monthCtrlDao.insertById(monthCtrl);
        
        //批量插入总包清单
        if(CollectionUtils.isNotEmpty(monthCtrlSubpacks)){
            monthCtrlSubpackDao.insertByBatch(monthCtrlSubpacks);
        }
        
        //批量插入子项
        if(CollectionUtils.isNotEmpty(monthCtrlSubs)){
            monthCtrlSubDao.insertByBatch(monthCtrlSubs);
        }
        
        //批量插入节点管控
        if(CollectionUtils.isNotEmpty(monthCtrlMiles)){
            monthCtrlMileDao.insertByBatch(monthCtrlMiles);
        }
        
        //批量插入附件
        if(CollectionUtils.isNotEmpty(monthCtrlAtts)){
            monthCtrlAttDao.insertByBatch(monthCtrlAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.IC_MONTH_CTRL;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, monthCtrl.getId(), monthCtrl.getProcessId(), null);
        
    }

    @Override
    public MonthCtrlVo selectById(String id) {
        return monthCtrlDao.selectById(id);
    }

    @Override
    public List<MonthCtrlDomain> selectByProCode(String proCode) {
        return monthCtrlDao.selectByProCode(proCode);
    }

}